package gupao.concurrency.simpleweb.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class CalcController {

    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

    @GetMapping("/calc")
    public String calc(){
        Thread d = new Thread();
        List<Long> params = doQuery();
        try{
            return String.valueOf(doCalc(params));
        }catch (Exception e){
//            e.printStackTrace();
        }
        return String.valueOf(params.stream().limit(1000).mapToDouble(Double::new).average());
    }

    private long doCalc(List<Long> list){
        throw new RuntimeException("模拟业务异常");
    }

    private List<Long> doQuery(){
        List<Long> list = new ArrayList<>();
        for(int i = 0; i < 100000; i++){
            Double d = Math.random() * 3000;
            list.add( d.longValue() + i);
        }
        return list;
    }
}
